package com.hd.trans.utils

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.text.TextUtils
import android.util.Base64
import android.util.Log
import java.io.*

object Base64Util {

    /*编码*/
    fun encodeBase64(encodeStr: String): String? {
        return if (encodeStr == null) null else try {
            val b = Base64.encode(encodeStr.toByteArray(), Base64.DEFAULT)
            String(b)
        } catch (e: Exception) {
            e.printStackTrace()
            null
        }
    }

    /*解码*/
    fun decodeBase64(decodeStr: String?): String? {
        return if (decodeStr == null) null else try {
            val b = Base64.decode(decodeStr, Base64.DEFAULT)
            String(b)
        } catch (e: Exception) {
            e.printStackTrace()
            null
        }
    }

    fun bitmap2Base64(bitmap: Bitmap): String {
        var result = ""
        var bos: ByteArrayOutputStream? = null
        try {
            bos = ByteArrayOutputStream()
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos)
            bos.flush()
            bos.close()
            result = Base64.encodeToString(bos.toByteArray(), Base64.DEFAULT)
        } catch (e: IOException) {
            e.printStackTrace()
        } finally {
            try {
                if (bos != null) {
                    bos.flush()
                    bos.close()
                }
            } catch (e: IOException) {
                e.printStackTrace()
            }
        }
        return result
    }

    fun imageToBase64(path: String?): String? {
        if (TextUtils.isEmpty(path)) {
            return null
        }
        var `is`: InputStream? = null
        var data: ByteArray? = null
        var result: String? = null
        try {
            `is` = FileInputStream(path)
            //创建一个字符流大小的数组。
            data = ByteArray(`is`.available())
            //写入数组
            `is`.read(data)
            //用默认的编码格式进行编码
            result = Base64.encodeToString(data, Base64.DEFAULT)
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        } finally {
            if (null != `is`) {
                try {
                    `is`.close()
                } catch (e: IOException) {
                    e.printStackTrace()
                }
            }
        }
        //jdk7中加密/解密带换行符；jdk8中则没有
        result = result?.replace("\n","")?.replace("\r","")
        //Log.i("imageToBase64","data:image/png;base64,$result")
        return "${getBase64Head(path!!)}$result"
        /*val bitmap = BitmapFactory.decodeFile(path,getBitmapOption(1))//宽高缩小比例1:X倍
        return "data:image/png;base64,${bitmap2Base64(bitmap)}"*/
    }

    fun getFileBase64(filePath: String): String?{
        var `is`: InputStream? = null
        var result: String? = null
        try {
            `is` = FileInputStream(filePath)
            //创建一个字符流大小的数组。
            val data = ByteArray(`is`.available())
            //写入数组
            `is`.read(data)
            //用默认的编码格式进行编码
            result = Base64.encodeToString(data, Base64.DEFAULT)
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        } finally {
            if (null != `is`) {
                try {
                    `is`.close()
                } catch (e: IOException) {
                    e.printStackTrace()
                }
            }
        }
        //jdk7中加密/解密带换行符；jdk8中则没有
        result = result?.replace("\n","")?.replace("\r","")
        return result
    }

    private fun getBase64Head(path: String): String {
        val head = getHeadDataByExtension(File(path).extension)
        Log.e("getBase64Head","$head")
        return head
    }

    fun getBase64Head(file: File): String {
        val head = getHeadDataByExtension(file.extension)
        Log.e("getBase64Head","$head")
        return head
    }

    private fun getBitmapOption(inSampleSize: Int): BitmapFactory.Options {
        System.gc()
        val options = BitmapFactory.Options()
        options.inPurgeable = true
        options.inSampleSize = inSampleSize
        return options
    }

    private fun getHeadDataByExtension(extension: String?): String{
        if (TextUtils.isEmpty(extension)){
            return "data:image/png;base64,"
        }
        return  when(extension?.toLowerCase()){
            "jpg","jpeg" -> "data:image/jpeg;base64,"
            "png" -> "data:image/png;base64,"
            "webp" -> "data:image/webp;base64,"
            "gif" -> "data:image/gif;base64,"
            "pdf" -> "data:application/pdf;base64,"
            "html" -> "data:text/html;base64,"
            "docx" -> "data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,"
            "doc","rtf" -> "data:application/msword;base64,"
            "xlsx" -> "data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,"
            "xls" -> "data:application/vnd.ms-excel;base64,"
            "ppt" -> "data:application/vnd.ms-powerpoint;base64,"
            "pptx" -> "data:application/vnd.openxmlformats-officedocument.presentationml.presentation;base64,"
            "zip" -> "data:application/x-zip-compressed;base64,"
            "mp4" -> "data:video/mp4;base64,"
            "txt" -> "data:text/plain;base64,"
            else -> "data:image/png;base64,"
        }
    }
}